package com.parleys.activity;

import java.util.List;

import roboguice.inject.InjectExtra;
import roboguice.inject.InjectView;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.google.inject.Inject;
import com.parleys.R;
import com.parleys.adapter.ParleysListAdapter;
import com.parleys.androidservice.ParleysMediaPlayer;
import com.parleys.domain.Channel;
import com.parleys.domain.ParleysListObject;
import com.parleys.domain.Presentation;
import com.parleys.domain.Space;
import com.parleys.exception.WebserviceNotAvailableException;
import com.parleys.helper.ApplicationConstants;
import com.parleys.helper.ConnectionErrorRunnable;
import com.parleys.helper.DownloadTask;
import com.parleys.helper.ParleysLoadingDialog;
import com.parleys.helper.ProcessResultListRunnable;
import com.parleys.service.intf.ParleysService;
import com.parleys.service.intf.PresentationAssetsService;

/**
 * Displays a list of presentations. Clicking on a presentation brings you to the details.
 * 
 * @author David Van Droogenbroeck
 * 
 */
public class PresentationListActivity extends AbstractParleysActivity {

	@InjectExtra(ApplicationConstants.SPACE_ARG)
	private Space space;

	@InjectExtra(ApplicationConstants.CHANNEL_ARG)
	private Channel channel;

	@InjectExtra(ApplicationConstants.ONLINE_ARG)
	private Boolean online;

	@Inject
	private ParleysService parleysService;

	@Inject
	private PresentationAssetsService presentationAssetsService;

	@InjectView(R.id.list)
	protected ListView listView;

	@InjectView(R.id.emptyText)
	private TextView emptyText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.parleys_list);

		setTitle(channel.getName());
		emptyText.setText(getString(R.string.no_presentations));

		listView.setOnItemClickListener(new PresentationOnClickHandler());
		listView.setOnItemLongClickListener(new PresentationOnLongClickHandler());

		final ProgressDialog progressDialog = new ParleysLoadingDialog(this);
		progressDialog.show();

		final PresentationAssetsService presentationAssetsService = this.presentationAssetsService;
		final ParleysService parleysService = this.parleysService;

		/**
		 * Offloading the fetching of the list data to a different thread
		 */
		Runnable fillList = new Runnable() {

			@Override
			public void run() {
				try {
					List<? extends ParleysListObject> list = parleysService.getPresentationsForChannel(channel.getId(),
							online);
					ParleysListAdapter parleysListAdapter = new ParleysListAdapter(PresentationListActivity.this, list,
							channel.getAssetBaseURL(), presentationAssetsService);

					PresentationListActivity.this.runOnUiThread(new ProcessResultListRunnable(list, listView,
							emptyText, parleysListAdapter, progressDialog));
				} catch (WebserviceNotAvailableException e) {
					PresentationListActivity.this.runOnUiThread(new ConnectionErrorRunnable(
							PresentationListActivity.this, progressDialog));
				}

			}
		};

		new Thread(fillList).start();

	}

	@Override
	protected void onResume() {
		super.onResume();
		ParleysMediaPlayer.stop();
	}

	private class PresentationOnClickHandler implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> listView, View layout, int arg2, long arg3) {
			Presentation presentation = (Presentation) ((ParleysListAdapter) listView.getAdapter()).getItem(arg2);
			gotoDetailActivity(space, channel, presentation, online);
		}

	}

	private class PresentationOnLongClickHandler implements OnItemLongClickListener {

		@Override
		public boolean onItemLongClick(AdapterView<?> listView, View arg1, int arg2, long arg3) {
			final Presentation presentation = (Presentation) ((ParleysListAdapter) listView.getAdapter()).getItem(arg2);

			final CharSequence onlineItems[] = { getString(R.string.general_info),
					getString(R.string.preview_chapters), getString(R.string.preview_slides),
					getString(R.string.download) };
			final CharSequence onlineItemsNoDownload[] = { getString(R.string.general_info),
					getString(R.string.preview_chapters), getString(R.string.preview_slides) };
			final CharSequence offlineItems[] = { getString(R.string.general_info), getString(R.string.chapters),
					getString(R.string.slides), getString(R.string.play) };
			CharSequence items[];

			if (online) {
				if (parleysService.isPresentationDownloaded(presentation.getId())) {
					items = onlineItemsNoDownload;
				} else {
					items = onlineItems;
				}
			} else {
				items = offlineItems;
			}

			AlertDialog.Builder builder = new AlertDialog.Builder(PresentationListActivity.this);
			builder.setTitle(presentation.getName());
			builder.setCancelable(true);
			builder.setItems(items, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int item) {
					if (item == 0) {
						gotoDetailActivity(space, channel, presentation, online);
					} else if (item == 1) {
						gotoChaptersActivity(space, channel, presentation, online);
					} else if (item == 2) {
						gotoSlidesActivity(space, channel, presentation, online);
					} else if (item == 3 && online) {
						DownloadTask task = new DownloadTask(presentation, channel, space);
						getInjector().injectMembers(task);
						task.start();
					} else if (item == 3 && !online) {
						ParleysMediaPlayer.toggle(channel, presentation, online);
						gotoSlidesActivity(space, channel, presentation, online);
					}
				}
			});

			builder.create().show();

			return true;
		}

	}

	@Override
	public boolean onSearchRequested() {
		Bundle appData = new Bundle();
		appData.putBoolean("test", true);
		startSearch("test", false, appData, false);
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem resetMenuItem = menu.add(0, 0, 0, getString(R.string.my_download_list));
		resetMenuItem.setIcon(android.R.drawable.ic_menu_save);
		return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		// goto my downloads
		if (item.getItemId() == 0) {
			startActivity(new Intent(this, MyDownloadsActivity.class));
		}
		return false;
	}

}
